var commodityId = $("#commodityId"),
        error_message = $("#error_message"),
        alertPanel = $(".alert"),
        page_url = decodeURI(location.href),
        lottery = $("#lottery"),
        sort = $("#sort"),
        start = page_url.indexOf("?");

// 初始化标的信息
var currLoan;
var lotteryId = $("input[name=id]").val();
if (lotteryId && lotteryId !== '') {
    $.get("pointsystem/lotteryGet/" + lotteryId, function (data) {
        if (data) {
            var lot = data;
            commodityId.val(lot.commodityId);
            lottery.val(lot.lottery);
            $('button.multiselect:first').prop("disabled", true);
            sort.val(lot.sort);
        }
    });
}

function showError(msg) {
    error_message.text(msg);
    alertPanel.show();
}
function hideError() {
    error_message.text('');
    alertPanel.hide();
}
$("input[type=text]").focus(function () {
    hideError();
});

/**
 * 提交表单
 * 
 * @param {type} param
 */
function saveLotteryBtn() {
    // 检验标的金额
    if (lottery.val() === '') {
        showError("请输入获奖基数");
        return;
    }
    if (lottery.val() > 10000) {
        showError("获奖基数不能大于10000!");
        return;
    }
    if (sort.val() === '') {
        showError("拍卖顺序不能为空");
        return;
    }
    if (isNaN(sort.val())) {
        showError("拍卖顺序必须为数字");
        return;
    }
    if (sort.val() == '0') {
        showError("拍卖顺序必须为大于0的数字");
        return;
    }
    if (sort.val() > 10) {
        showError("拍卖顺序必须为小于等于10的数字");
        return;
    }
    var groupid = $("input[name=groupid]").val();
    var id = $("input[name=id]").val();
    $.get("pointsystem/countBySortLottery", {"sort": sort.val(), "groupid": groupid, "id": id}, function (result) {
        if (result.success) {
            showError("抽奖顺序已经存在，请重新选择抽奖顺序，抽奖范围为1--10!");
            return false;
        } else {
            // 保存标的信息
            var data = sys.formToJson($("#loanForm"));
            $.post("pointsystem/saveLottery", data, function (result) {
                if (result.success) {
                    sys.alert(result.message, function () {
                        location.href = "pointsystem/lotterylist";
                    });

                } else {
                    sys.alert(result.message);
                }
            }).fail(function () {
                alert("服务器错误");
            });
        }
    });
}